package com.example.meituan.activity.user;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.meituan.R;
import com.example.meituan.activity.bean.AdminBean;
import com.example.meituan.activity.bean.BusinessBean;
import com.example.meituan.activity.bean.UserAddressBean;
import com.example.meituan.activity.bean.UserBean;
import com.example.meituan.activity.user.adapter.AddressAdapter;
import com.example.meituan.activity.user.adapter.ShopCarAdapter;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.dao.UserAddressDao;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class UserAddressActivity extends AppCompatActivity implements OnItemClickType {
    AdminBean currentUser;
    String userId;
    androidx.recyclerview.widget.RecyclerView recyclerView;
    AddressAdapter adapter;
    List<UserAddressBean> userAddressBeans;
    String businessId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_user_address);

        currentUser = AdminDao.getUserInfo(UserAddressActivity.this);
        userId = currentUser.getUserId();
        businessId = (String) getIntent().getSerializableExtra("businessBean");
        recyclerView = findViewById(R.id.user_address_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refresh();

        TextView back = findViewById(R.id.user_address_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();//返回功能
                //finish();
            }
        });
        TextView add = findViewById(R.id.user_address_add_my);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = LayoutInflater.from(UserAddressActivity.this);
                View dialogView = inflater.inflate(R.layout.dialog_address_info, null);

                final EditText recipientEditText = dialogView.findViewById(R.id.recevie_name);
                final EditText phoneEditText = dialogView.findViewById(R.id.text_view_phone);
                final EditText addressEditText = dialogView.findViewById(R.id.text_view_address);

                new AlertDialog.Builder(UserAddressActivity.this)
                        .setView(dialogView)
                        .setPositiveButton("添加", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UserAddressBean userAddressBean = new UserAddressBean();
                                String id = UUID.randomUUID().toString().replace("-", "");
                                String newRecipient = recipientEditText.getText().toString();
                                String newPhone = phoneEditText.getText().toString();
                                String newAddress = addressEditText.getText().toString();
                                userAddressBean.setS_id(id);
                                userAddressBean.setS_name(newRecipient);
                                userAddressBean.setS_phone(newPhone);
                                userAddressBean.setS_address(newAddress);
                                userAddressBean.setS_user_id(userId);
                                int a = UserAddressDao.insertUserAddress(userAddressBean);
                                if (a == 1) {
                                    Toast.makeText(UserAddressActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                    refresh();
                                } else {
                                    Toast.makeText(UserAddressActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
            }
        });
        TextView manage = findViewById(R.id.user_address_manage_my);
        manage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(UserAddressActivity.this)
                        .setTitle("确认删除")
                        .setMessage("确定要删除这些吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                for (UserAddressBean userAddressBean : userAddressBeans) {
                                    int a = UserAddressDao.deleteUserAddress(userAddressBean.getS_id());
                                    if (a == 1) {
                                        Toast.makeText(UserAddressActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                        refresh();
                                    } else {
                                        Toast.makeText(UserAddressActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
            }
        });
    }

    @Override
    public void onItemClick(UserAddressBean userAddressBean) {
        if (businessId != null) {
            Intent resultIntent = new Intent();
            resultIntent.putExtra("userAddress", userAddressBean);
            setResult(RESULT_OK, resultIntent);
            finish();
        }
    }

    @Override
    public void onItemUpder(UserAddressBean userAddressBean) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View dialogView = inflater.inflate(R.layout.dialog_address_info, null);

        final EditText recipientEditText = dialogView.findViewById(R.id.recevie_name);
        final EditText phoneEditText = dialogView.findViewById(R.id.text_view_phone);
        final EditText addressEditText = dialogView.findViewById(R.id.text_view_address);

        recipientEditText.setText(userAddressBean.getS_name());
        phoneEditText.setText(userAddressBean.getS_phone());
        addressEditText.setText(userAddressBean.getS_address());

        new AlertDialog.Builder(this)
                .setView(dialogView)
                .setPositiveButton("修改", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String newRecipient = recipientEditText.getText().toString();
                        String newPhone = phoneEditText.getText().toString();
                        String newAddress = addressEditText.getText().toString();

                        userAddressBean.setS_name(newRecipient);
                        userAddressBean.setS_phone(newPhone);
                        userAddressBean.setS_address(newAddress);

                        int a = UserAddressDao.updateUserAddress(userAddressBean);
                        if (a == 1) {
                            Toast.makeText(UserAddressActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            refresh();
                        } else {
                            Toast.makeText(UserAddressActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .setNegativeButton("取消", null)
                .setNeutralButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        int a = UserAddressDao.deleteUserAddress(userAddressBean.getS_id());
                        if (a == 1) {
                            Toast.makeText(UserAddressActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                            refresh();
                        } else {
                            Toast.makeText(UserAddressActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .show();
    }

    @Override
    public void ifChecked(UserAddressBean userAddressBean, boolean isChecked) {
        if (isChecked && userAddressBeans != null) {
            userAddressBeans.add(userAddressBean);
        }
    }

    public void refresh() {
        userAddressBeans = UserAddressDao.getUserAddressesByUserId(userId);
        if (userAddressBeans == null || userAddressBeans.isEmpty()) {
            adapter = new AddressAdapter(new ArrayList<>(), this);
            Toast.makeText(this, "没有数据显示", Toast.LENGTH_SHORT).show();
            recyclerView.setAdapter(adapter);
        } else {
            adapter = new AddressAdapter(userAddressBeans, this);
            recyclerView.setAdapter(adapter);
        }
    }
}